<?php

/**
 * This is the model class for table "language".
 *
 * The followings are the available columns in table 'language':
 * @property integer $id
 * @property integer $num
 * @property string $code
 * @property string $name
 * @property integer $sort
 * @property integer $status
 */
class Language extends CActiveRecord
{
    private $__default;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'language';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('num, code, name', 'required'),
			array('num, sort, status', 'numerical', 'integerOnly'=>true),
			array('code', 'length', 'max'=>4),
			array('name', 'length', 'max'=>128),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, num, code, name, sort, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'num' => 'Num',
			'code' => 'Code',
			'name' => 'Name',
			'sort' => 'Sort',
			'status' => 'Status',
		);
	}

    public function getAll()
    {
        return $this->findAllByAttributes(array('status'=>1));
    }

    public function getLang($code)
    {
        return $this->findAllByAttributes(array(
            'code'=>$code,
            'status'=>1,
        ));
    }

    public function getCurrentId($code='')
    {
        if(!$code) $code=Yii::app()->language;
        $lang=$this->findByAttributes(array('code'=>$code));

        if($lang!==null) return $lang->id;
        else return false;
    }

    public function getDefaultLang()
    {
        if($this->__default===null) $this->__default=Setting::getConfig('default_language');
        return $this->__default;
    }

    public function getLangCode($id)
    {
        $lang=$this->findByPk($id);
        if($lang===null) return false;
        else return $lang->code;
    }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('num',$this->num);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('sort',$this->sort);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Language the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
